package cn.gogetter.classifiers;

import java.io.File;
import weka.classifiers.trees.M5P;
import weka.core.Instances;
import weka.core.Utils;
import weka.core.converters.ArffLoader;

public class ClassifiersM5P {
	public static void main(String[] args) throws Exception{
		// 加载数据
		Instances data = null;
		Instances test = null;
		String path = new File("").getAbsolutePath()+"\\data\\";
		ArffLoader loader = new ArffLoader();
		loader.setFile(new File(path + "house.arff"));
		data = loader.getDataSet();
		loader.setFile(new File(path + "house2.arff"));
		test = loader.getDataSet();
		data.setClass(data.attribute(5));
		test.setClass(test.attribute(5));
		data.setClassIndex(data.numAttributes() - 1);
		test.setClassIndex(test.numAttributes() - 1);
		M5P classifier = new M5P();
		String[] options = new String[2];
		options[0] = "-M";
		options[1] = "5";
		classifier.setOptions(options);
		// 训练
		classifier.buildClassifier(data);
		System.out.println("M5P模型："+classifier);
		System.out.println("i："+test.numAttributes());
		for(int i = 0; i < test.numInstances(); i++){
			System.out.println(i+"测试数据:"+test.get(i));
			//得到预测值
			double pred = classifier.classifyInstance(test.instance(i));
			System.out.println("预测值:"+pred);
			//得到实际值
			
			System.out.println("实际值:"+test.instance(i).toString(test.classIndex()));
			//得到分布
			double[] dist = classifier.distributionForInstance(test.instance(i));
			System.out.println("分布值:"+Utils.arrayToString(dist));
			
		}
	}
}
